//
//  LeftOfPic.m
//  HYHoney
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LeftOfPic.h"
#import "RESideMenu.h"
#import "NetworkHandeler.h"
#import "WaterFlowLayout.h"
#import "UIImageView+WebCache.h"
#import "ModelOfPBL.h"
#import "CollectionView.h"
#import "NetworkingHandle.h"
#import "PushPicNR.h"
#import "DKNightVersion.h"

@interface LeftOfPic ()<NetworkHandelerDelegate, WaterFlowDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *arrImage;
@property (nonatomic, retain) NSMutableDictionary *dicX;
@property (nonatomic, retain) NSString *str;
@property (nonatomic, assign) NSInteger count1;

@end

@implementation LeftOfPic

- (void)dealloc{

    [_arr release];
    [_collectionView release];
    [_arrImage release];
    [_dicX release];
    [_str release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handleData];
    
    self.navigationItem.title = @"装修图库";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1 green:1 blue:1 alpha:1], NSForegroundColorAttributeName, nil]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xial.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    WaterFlowLayout *flowLayout = [[WaterFlowLayout alloc] init];
    
    flowLayout.delegate = self;
    
    CGFloat w = ([UIScreen mainScreen].bounds.size.width - 30) / 2;
    flowLayout.itemSize = CGSizeMake(w, 0);
    flowLayout.insertItemSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[CollectionView class] forCellWithReuseIdentifier:@"CELL"];
    
}



- (void)handleData{
    
    NSString *str = @"http://appapi.17house.com/xiaoguotuApi.php?action=albumList&page=1&pageSize=20&tagid=1";
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
}


- (void)handelerDidComplete:(id)result{
    
    self.arr = [NSMutableArray array];
    
    NSArray *array = [result objectForKey:@"data"];
    
    for (NSDictionary *dic in array) {
        
        ModelOfPBL *model = [[ModelOfPBL alloc] init];
        
        model.title2 = [dic objectForKey:@"title"];
        model.url = [dic objectForKey:@"url"];
        model.desc = [dic objectForKey:@"desc"];
        model.idNumber = [dic objectForKey:@"id"] ;
        
        [self.arr addObject:model];
        [model release];
    }
    
    
    if (!self.arrImage) {
        
        self.arrImage = [NSMutableArray array];
    }
    
    
    // 创建一个新数组, 里面初始化放置占位图image, 个数和model所在数组一样.
    for (NSInteger i = self.count1; i < self.arr.count; i++) {
        
        [self.arrImage addObject:[UIImage imageNamed:@"jzz"]];
    }
    
    self.count1 = self.arr.count;
    
    
    
    for (ModelOfPBL *model2 in self.arr) {
        
        NSInteger index = [self.arr indexOfObject:model2];
        
        // 异步加载图片, 同时传入model的index, 当图片下载完之后, 在arrImage数组中, 根据index替换原图片(占位图).
        [NetworkingHandle networkingWithURL:model2.url withIndex:index completeHandel:^(NSURLResponse *response, NSData *data, NSInteger indexInsert) {

            UIImage *image = [UIImage imageWithData:data];
            
            [self.arrImage replaceObjectAtIndex:index withObject:image];
            
            [self.collectionView reloadData];
        }];
        
    }
    
    
    [self.collectionView reloadData];
}



- (CGFloat)heightForItemIndexPath:(NSIndexPath *)indexPath{
    
    return [CollectionView height:self.arrImage[indexPath.item]] + 20 + 40;
}



- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section{
    
    return self.arrImage.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    // 图片加载后上数据
    CollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
    cell.imageV.image = [self.arrImage objectAtIndex:indexPath.item];
    
    ModelOfPBL *model = [[ModelOfPBL alloc] init];
    
    model = self.arr[indexPath.item];
    
    cell.labelForDesc.text = model.desc;
    cell.labelForDesc.dk_textColorPicker = DKColorWithRGB(0x000000, 0x87ceff);
    cell.labelForTitle.text = model.title2;
    cell.labelForTitle.dk_textColorPicker = DKColorWithRGB(0x000000, 0xF0FFFF);
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PushPicNR *picNR = [[PushPicNR alloc] init];
    
    ModelOfPBL *model = [[ModelOfPBL alloc] init];
    
    model = self.arr[indexPath.item];
    picNR.idNumber = [model.idNumber intValue];
    
    [self.navigationController pushViewController:picNR animated:NO];
    
    [picNR release];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
